package com.example.hzq.zqb.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.CampusshopActivity;
import com.example.hzq.zqb.activity.EnterpriseActivity;
import com.example.hzq.zqb.activity.LoginActivity;
import com.example.hzq.zqb.activity.MakemoneyActivity;
import com.example.hzq.zqb.activity.MyshopActivity;
import com.example.hzq.zqb.adapter.NetworkImageHolderView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.Arrays;
import java.util.List;

import static com.example.hzq.zqb.activity.MainActivity.loginflag;

/**
 * Created by hzq on 2017/7/6.
 */

public class Fragment1 extends Fragment implements View.OnClickListener,OnItemClickListener{
    public static String imageurl="http://www.hongyudasha.com/zqb/Public/goodsImg/";
    private ConvenientBanner convenientBanner;//顶部广告栏控件
    private List<String> networkImages;
    private String[] image={imageurl + "campus1.png",imageurl + "campus2.jpg",imageurl + "campus3.jpg"};
    private LinearLayout layout1,layout2,layout3,layout4;
    private View view=null;
    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment1,container,false);
        context=getActivity();
        convenientBanner = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
        layout1=(LinearLayout)view.findViewById(R.id.campus);
        layout2=(LinearLayout)view.findViewById(R.id.company);
        layout3=(LinearLayout)view.findViewById(R.id.mycreate);
        layout4=(LinearLayout)view.findViewById(R.id.makemoney);
        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layout4.setOnClickListener(this);
        netinit();
        convenientBanner.startTurning(2500);
        return view;
    }
    private void initImageLoader(){
        //网络图片例子,结合常用的图片缓存库UIL,你可以根据自己需求自己换其他网络图片库
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().
                showImageForEmptyUri(R.drawable.ic_default_adimage)
                .cacheInMemory(true).cacheOnDisk(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context.getApplicationContext()).defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }
    private void netinit() {
        initImageLoader();
        networkImages = Arrays.asList(image);
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages).setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setOnItemClickListener(this);

    }
    @Override
    public void onItemClick(int position) {
        Toast.makeText(context,"点击了第"+(position+1)+"个",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.campus:
                if (loginflag){
                    Intent intent=new Intent();
                    intent.setClass(context, CampusshopActivity.class);
                    context.startActivity(intent);
                }

                else {
                    Intent intent=new Intent();
                    intent.setClass(context, LoginActivity.class);
                    context.startActivity(intent);
                }
                break;
            case R.id.company:
                if (loginflag){
                    Intent intent=new Intent();
                    intent.setClass(context, EnterpriseActivity.class);
                    context.startActivity(intent);
                }

                else {
                    Intent intent=new Intent();
                    intent.setClass(context, LoginActivity.class);
                    context.startActivity(intent);
                }
                break;
            case R.id.mycreate:
                if (loginflag){
                    Intent intent=new Intent();
                    intent.setClass(context,MyshopActivity.class);
                    context.startActivity(intent);
                }

                else {
                    Intent intent=new Intent();
                    intent.setClass(context, LoginActivity.class);
                    context.startActivity(intent);
                }
                break;
            case R.id.makemoney:
                if (loginflag){
                    Intent intent=new Intent();
                    intent.setClass(context, MakemoneyActivity.class);
                    context.startActivity(intent);
                }

                else {
                    Intent intent=new Intent();
                    intent.setClass(context, LoginActivity.class);
                    context.startActivity(intent);
                }
                break;
        }
    }
}
